//
//  BKUserInputNumView.m
//  bestkeep
//
//  Created by windCould on 16/11/16.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKUserInputNumView.h"
#import "BKMyCommon.h"

@interface BKUserInputNumView ()<UITextFieldDelegate>

@property (nonatomic,strong)UIImageView * leftImgV;
@property (nonatomic,strong)UIButton * rightBtn;
@property (nonatomic,strong)UIView * headerLineView;
@property (nonatomic,strong)UIView * bottomLineView;
@property (nonatomic,strong)UIControl * changeType; //点击用来展示 用户账号记录


@end




@implementation BKUserInputNumView


-(instancetype)initWithFrame:(CGRect)frame leftIcon:(UIImage *)leftImg rightIcons:(NSArray<UIImage *> *)rightImgs usageType:(InputviewUsageType)usageType
{
   
   self = [self initWithFrame:frame];
   self.leftImgV.image = leftImg;
   self.usageType = usageType; 
   self.rightBtn.userInteractionEnabled = rightImgs == nil ? NO:YES;
    
    if (rightImgs.count >= 1) {
        
       [self.rightBtn setBackgroundImage:rightImgs[0] forState:UIControlStateNormal];
    }
    
    if (rightImgs.count >1 ){
    
       [self.rightBtn setBackgroundImage:rightImgs[1] forState:UIControlStateSelected];
    }
   
  //  [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getInputContent) name:UITextFieldTextDidEndEditingNotification object:nil];
    
    return self;
}

-(void)setPlaceHolderString:(NSString *)placeHolderString
{
    _placeHolderString = placeHolderString;
    _userNumTextField.placeholder = _placeHolderString;
}

-(void)setIsSecurity:(BOOL)isSecurity
{
    _isSecurity = isSecurity;
    self.userNumTextField.secureTextEntry = _isSecurity;
}

-(void)setKeyTpe:(UIKeyboardType)keyTpe
{
    _keyTpe = keyTpe;
    self.userNumTextField.keyboardType = _keyTpe;
}

-(void)setEndEditing:(BOOL)endEditing
{
    _endEditing = endEditing;
    [self.userNumTextField resignFirstResponder];
}

-(void)setContentStr:(NSString *)contentStr
{
    _contentStr = contentStr;
    if (_contentStr != nil && ![_contentStr isEqualToString:@""]) {
        
        self.userNumTextField.text = _contentStr;
    }
    
}

-(void)setBordLineShowType:(BKUserInputNumViewLineShowType)bordLineShowType
{
    if (bordLineShowType == HideHeaderLine) {
        
        _headerLineView.hidden = YES;
        _bottomLineView.hidden = NO;
    }

    if (bordLineShowType == HideBottomLine) {
        
        _headerLineView.hidden = NO;
        _bottomLineView.hidden = YES;
    }
    
    if (bordLineShowType == HideBottomLineAndBottomLine) {
        
        _bottomLineView.hidden = YES;
        _headerLineView.hidden = YES;
    }

}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}



#pragma mark =========================Fuction===================================

//点击右按钮
-(void)rightBtnClick
{
    if (self.rightBtn.userInteractionEnabled) {
        
         [self rightBtnClick:_rightBtn];
    }
    
}

-(void)rightBtnClick:(UIButton *)btn
{
//    return;
//    
//    if (btn.isSelected) {
//        
//        btn.selected = NO;
//    } else {
//    
//        btn.selected = YES;
//    }
//    
//    if ([self.myDelegate respondsToSelector:@selector(bKUserInputNumView:btnSelect:)]) {
//        
//        [self.myDelegate bKUserInputNumView:self btnSelect:btn.selected];
//    }

}

//-(void)getInputContent
//{
//    
//    if ([self.myDelegate respondsToSelector:@selector(bKUserInputNumView:inputTextFieldContent:)]) {
//        
//        [self.myDelegate bKUserInputNumView:self inputTextFieldContent:self.userNumTextField.text];
//    }
//    
//}


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        [self setUpViews];
    }
    
    return  self;
}

-(void)setUpViews{
    
    
    _leftImgV = [[UIImageView alloc]initWithFrame:CGRectZero];
    _leftImgV.contentMode = UIViewContentModeCenter;
    [self addSubview:_leftImgV];
    
    _userNumTextField = [[UITextField alloc]initWithFrame:CGRectZero];
    _userNumTextField.font = UIFontCustom(17);
    _userNumTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _userNumTextField.delegate = self;
    [self addSubview:_userNumTextField];
    
    _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightBtn.selected = NO;
    [_rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_rightBtn];
    
    _headerLineView = [[UIView alloc]initWithFrame:CGRectZero];
    _headerLineView.backgroundColor = HexColor(0xdddddd);
    [self addSubview:_headerLineView];
    
    _bottomLineView = [[UIView alloc]initWithFrame:CGRectZero];
    _bottomLineView.backgroundColor = HexColor(0xdddddd);
    [self addSubview:_bottomLineView];
    
    _changeType = [[UIControl alloc]initWithFrame:CGRectZero];
    [_changeType addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_changeType];
    
    
    [self layoutViews];
    
}


-(void)layoutViews{
    
    [_leftImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.mas_left).offset(53);
        make.centerY.mas_equalTo(self.center.y).offset(0);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
   
    [_userNumTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(_leftImgV.mas_right).offset(10);
        make.centerY.mas_equalTo(_leftImgV.mas_centerY).offset(0);
        make.right.mas_equalTo(_rightBtn.mas_left).offset(20);
        make.height.mas_equalTo(@(40));
    }];
    
    [_rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self.mas_right).offset(-48);
        make.centerY.mas_equalTo(_leftImgV.mas_centerY).offset(0);
        make.size.mas_equalTo(CGSizeMake(20,20));
    }];
    
//    [_changeType mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.right.mas_equalTo(self.mas_right).offset(0);
//        make.centerY.mas_equalTo(_leftImgV.mas_centerY).offset(0);
//        make.left.mas_equalTo(_rightBtn.mas_left).offset(-8);
//        
//    }];
    
    
    [_headerLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(@(0.5));
        make.left.mas_equalTo(self.mas_left).offset(33);
        make.right.mas_equalTo(self.mas_right).offset(-33);
        make.top.mas_equalTo(self.mas_top).offset(0);
        
    }];
    
    [_bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(@(0.5));
        make.left.mas_equalTo(self.mas_left).offset(33);
        make.right.mas_equalTo(self.mas_right).offset(-33);
        make.bottom.mas_equalTo(self.mas_bottom).offset(0);
        
    }];
    
}


-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidEndEditingNotification object:self.userNumTextField];
}




@end
